// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.network.packets;

import java.nio.ByteBuffer;
import zombie.characters.IsoGameCharacter;
import zombie.characters.IsoPlayer;
import zombie.characters.BodyDamage.BodyPart;
import zombie.characters.skills.PerkFactory;
import zombie.core.network.ByteBufferWriter;
import zombie.core.raknet.UdpConnection;
import zombie.debug.DebugLog;
import zombie.inventory.InventoryItem;
import zombie.inventory.types.DrainableComboItem;
import zombie.inventory.types.Food;
import zombie.network.GameClient;
import zombie.network.PacketValidator;
import zombie.network.packets.hit.Player;
import zombie.network.packets.hit.PlayerBodyPart;
import zombie.network.packets.hit.PlayerItem;

public class Disinfect implements INetworkPacket {
    protected final Player wielder = new Player();
    protected final Player target = new Player();
    protected PlayerBodyPart bodyPart = new PlayerBodyPart();
    protected PlayerItem alcohol = new PlayerItem();

    public void set(IsoGameCharacter _wielder, IsoGameCharacter _target, BodyPart _bodyPart, InventoryItem _alcohol) {
        this.wielder.set(_wielder);
        this.target.set(_target);
        this.bodyPart.set(_bodyPart);
        this.alcohol.set(_alcohol);
    }

    @Override
    public void parse(ByteBuffer b, UdpConnection connection) {
        this.wielder.parse(b, connection);
        this.wielder.parsePlayer(connection);
        this.target.parse(b, connection);
        this.target.parsePlayer(null);
        this.bodyPart.parse(b, this.target.getCharacter());
        this.alcohol.parse(b, connection);
    }

    @Override
    public void write(ByteBufferWriter b) {
        this.wielder.write(b);
        this.target.write(b);
        this.bodyPart.write(b);
        this.alcohol.write(b);
    }

    public void process() {
        int _int = this.wielder.getCharacter().getPerkLevel(PerkFactory.Perks.Doctor);
        if (!this.wielder.getPlayer().isAccessLevel("None")) {
            _int = 10;
        }

        this.bodyPart.getBodyPart().setAlcoholLevel(this.bodyPart.getBodyPart().getAlcoholLevel() + this.alcohol.getItem().getAlcoholPower());
        float _float = this.alcohol.getItem().getAlcoholPower() * 13.0F - (float)(_int / 2);
        this.bodyPart.getBodyPart().setAdditionalPain(this.bodyPart.getBodyPart().getAdditionalPain() + _float);
        if (this.alcohol.getItem() instanceof Food) {
            Food food = (Food)this.alcohol.getItem();
            food.setThirstChange(food.getThirstChange() + 0.1F);
            if (food.getBaseHunger() < 0.0F) {
                food.setHungChange(food.getHungChange() + 0.1F);
            }
        }

        if ((double)this.alcohol.getItem().getScriptItem().getThirstChange() > -0.01
            || (double)this.alcohol.getItem().getScriptItem().getHungerChange() > -0.01) {
            this.alcohol.getItem().Use();
        } else if (this.alcohol.getItem() instanceof DrainableComboItem) {
            this.alcohol.getItem().Use();
        }
    }

    @Override
    public boolean isConsistent() {
        return this.wielder.getCharacter() != null
            && this.wielder.getCharacter() instanceof IsoPlayer
            && this.target.getCharacter() != null
            && this.target.getCharacter() instanceof IsoPlayer
            && this.bodyPart.getBodyPart() != null
            && this.alcohol.getItem() != null;
    }

    public boolean validate(UdpConnection connection) {
        if (GameClient.bClient && this.alcohol.getItem().getAlcoholPower() <= 0.0F) {
            DebugLog.General.warn(this.getClass().getSimpleName() + ": Validate error: " + this.getDescription());
            return false;
        } else {
            return PacketValidator.checkShortDistance(connection, this.wielder, this.target, this.getClass().getSimpleName());
        }
    }

    @Override
    public String getDescription() {
        String string = "\n\t" + this.getClass().getSimpleName() + " [";
        string = string + "wielder=" + this.wielder.getDescription() + " | ";
        string = string + "target=" + this.target.getDescription() + " | ";
        string = string + "bodyPart=" + this.bodyPart.getDescription() + " | ";
        return string + "alcohol=" + this.alcohol.getDescription() + "] ";
    }
}
